﻿using MuseumApp.Data;
using MuseumApp.Data.Models;
using MuseumApp.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace MuseumApp.Controllers
{
    public class MuseumsController : ApiController
    {

        private MuseumDbContext db = new MuseumDbContext();

        /* GET api/<controller>     */
        public IEnumerable<Museum> Get()
        {
            //return db.Reviews;

            //mList.Add(new Museum { MuseumId = 1, City = "Htown", Description = "3Lorum Ipsum Dolo", Name = "MOMA Houston" });
            //mList.Add(new Museum { MuseumId = 2, City = "Dallas", Description = "2Lorum Ipsum Dolo", Name = "MOMA Dallas" });
            //mList.Add(new Museum { MuseumId = 2, City = "Austin", Description = "1Lorum Ipsum Dolo", Name = "MOMA Austin" });

            using (MuseumDbContext db = new MuseumDbContext())
            {
                List<Museum> mList = new List<Museum>();
                mList = db.Museums
                    .ToList();
                return mList;
            }
        }


        [Route("api/museums/search/{str}")]
        [HttpGet]
        public IEnumerable<Museum> GetMuseumWithSearch(string str)
        {
            using (MuseumDbContext db = new MuseumDbContext())
            {
                string tStr = str.ToLower();
                List<Museum> mList = new List<Museum>();
                mList = db.Museums
                    .Where(s => s.Name.ToLower().Contains(tStr) || s.Description.ToLower().Contains(tStr))
                    .ToList();
                return mList;
            }
        }
        


        // GET api/<controller>/5
        public IHttpActionResult Get(int id)
        {
            List<MuseumReviewViewModel> rList = new List<MuseumReviewViewModel>();
            using (MuseumDbContext db = new MuseumDbContext())
            {
                var hasReviews = db.Museums.Where(r => r.MuseumId == id);
                rList = db.Museums
                    .Where(r => r.MuseumId == id)
                    .Select(r => new MuseumReviewViewModel()
                    {
                        Name = r.Name,
                        City = r.City,
                        Description = r.Description,
                        //Reviews = db.Reviews.Where(x => x. == id).Select(x => new Re(){

                        Reviews = r.Reviews.ToList(),
                        Tags = db.Museumtags.Where(t => t.MuseumId == id).Select(t => t.Tag).ToList(),
                        Rating = (r.Reviews.Count > 0) ? r.Reviews.Average(x => x.Rating) : 0,
                        MuseumId = r.MuseumId
                    })
                    .ToList();
            }
            return Ok(rList);
        }

        public void AddReview([FromBody]string Username)
        {
            Debug.Write("WEBAPI!");
            /*
            using (MuseumDbContext db = new MuseumDbContext())
            {
                Review tr = new Review();
                tr.Username 
            }
             */
        }

        // POST api/<controller>
        [HttpPost]
        //public void Post([FromBody]Review value)
        public IHttpActionResult Post(NewReviewViewModel rvm)
        {
            using (MuseumDbContext db = new MuseumDbContext())
            {
                Review newReview = new Review()
                {
                    Comment = rvm.Comment,
                    Username = rvm.Reviewer,
                    Rating = rvm.Rating,
                    MuseumId = rvm.MuseumId
                };
                db.Reviews.Add(newReview);
                db.SaveChanges();
                return Ok();
            }
        }

        // PUT api/<controller>/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/<controller>/5
        public void Delete(int id)
        {
            //
            var x = id;
        }

        //ajax path: 'api/museums/deleteReview/4'
        [Route("api/museums/deleteReview/{rid:int}")]
        [HttpDelete]
        public IHttpActionResult doDeleteReview(int rid)
        {
            using (MuseumDbContext db = new MuseumDbContext())
            {
                var query = db.Reviews.FirstOrDefault(r => r.ReviewId == rid);
                db.Reviews.Remove(query);
                db.SaveChanges();
                return Ok();
            }
            return Ok();
            
        }

    }
}